<?php

/*
 * This file will create the track document.
 * 
 * Created by: Peter Agno Jr.
 * Date created: Octber 23, 2011
 */
    
?>

<!-- START - ##############DISPLAY TRACK DOCUMENT############## -->
    <?php
        $trackDocInfo = "";
        
        $trackDocInfo = $trackDocInfo . "<td colspan='9' class='trackDocTd' id='mousePtrSpace' style='text-align: ";
        if ( $pageCalled == "mainPgTrckDoc" )
            $trackDocInfo = $trackDocInfo . "left";
        else
            $trackDocInfo = $trackDocInfo . "center";

        $trackDocInfo = $trackDocInfo . ";'>";
    //    $trackDocInfo = $trackDocInfo . "<div class='trackCirclesScroll divTrckScroll__".$documentId."' style='width:100%; height:100%; overflow-y:hidden; overflow-x:scroll;'><div style='width: 100%;'>";
        $trackDocInfo = $trackDocInfo . "<div><div>";
        
        // Get total route steps from the document route assigned to the documentId 308
        $trackDocInfo = $trackDocInfo . "<table class='trackOuterTableBorder' style='border-top: 0px; margin-bottom : 0px;'>"; // Start of outer table

        // Add the initial route first
        $trackDocInfo = $trackDocInfo . "<tr>"; // Start of first row
        $trackDocInfo = $trackDocInfo . "<td style='text-align:center; padding-left: 0px; padding-right: 0px; border: 0px; padding-bottom: 0px;'>"; // Initial Route TD

        // Start - Prepare the variable for the $query if the status of the main document is Rejected or Cancelled
        $benchMarkStepId = "";
        $theSubRouteId = "";
        $theAction = ""; // This will be used for the track points of subroutes

        // Get first the stepId that will serve as the benchmark to determine the value the stepId will be less than
        if ( isRejected($documentId) == true ) {
            $benchMarkStepId = getStepId($documentId, "Rejected");
            $theSubRouteId = "REJECTTRAC";
            $theAction = "Rejected";
        }
        else if ( isCancelled($documentId) == true ) {
            $benchMarkStepId = getStepId($documentId, "Cancelled");
            $theSubRouteId = "CANCELTRAC";
            $theAction = "Cancelled";
        }
        else {

        }

        // Check if the document was rejected or cancelled in initial route
        $performUnion = false;
        $rejOrCnclInInit = false;
        if ( $benchMarkStepId != "" && rejOrCnclInInitRoute($benchMarkStepId) == true ) {
            $performUnion = true;
            $rejOrCnclInInit = true;
        }

        // Prepare the variables for union
        $upperUnion = "";
        $lowerUnion = "";
        if ( $performUnion == true ) {
            $upperUnion = "( ";
            $benchMarkStepId = "AND stepId <= ".$benchMarkStepId;
            $lowerUnion = 
            "
            )
            UNION
            (
                -- Filter route steps, that is within the subroute
                SELECT *
                FROM 

                    -- Get all route steps
                    (SELECT mainDocumentId, currentStepId, WithRouteId.routeId as routeId, stepId, subRouteId, category
                    FROM

                        -- Get routeId
                        (SELECT mainDocumentId, currentStepId, routeId
                        FROM MainDocument as MD, RouteStep as RS
                        WHERE MD.currentStepId = RS.stepId AND mainDocumentId = '$documentId') as WithRouteId

                    LEFT JOIN RouteStep as RS
                    ON WithRouteId.routeId = RS.routeId) as UnfilteredRouteStep

                WHERE subRouteId = '$theSubRouteId'
                ORDER BY stepId
            )
            ";
        }
        else {
            $benchMarkStepId = "";
        }

        // End - Prepare the variable for the $query if the status of the main document is Rejected or Cancelled

        // Query the initial route, along with the reject and cancel points if needed.
        $query =
            "
                ".$upperUnion."
                -- Filter route steps, that is within the subroute
                SELECT *
                FROM 

                    -- Get all route steps
                    (SELECT mainDocumentId, currentStepId, WithRouteId.routeId as routeId, stepId, subRouteId, category
                    FROM

                        -- Get routeId
                        (SELECT mainDocumentId, currentStepId, routeId
                        FROM MainDocument as MD, RouteStep as RS
                        WHERE MD.currentStepId = RS.stepId AND mainDocumentId = '$documentId') as WithRouteId

                    LEFT JOIN RouteStep as RS
                    ON WithRouteId.routeId = RS.routeId) as UnfilteredRouteStep

                WHERE subRouteId IS NULL ".$benchMarkStepId."
                ORDER BY stepId
                ".$lowerUnion."
            ";

        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

        // Determine total number of steps, and current step of the document
        $totalStepCount = 0;
        $currentStep = 0;
        $contCheck = true;
        $stepIdStorage = array();
        $ctgryStorage = array();

        while ($row = mysql_fetch_array($result)) {
            $totalStepCount++;

            // Store the stepId which will be used for the id of the green/grey circles
            array_push($stepIdStorage, $row['stepId']);
            $ctgryStorage[] = $row['category']; // Store the category

            if( ( $row['currentStepId'] == $row['stepId'] ) && ( $contCheck == true ) ) {
                $currentStep = $totalStepCount;
                $contCheck = false;
            }
        }

        $archiveTotalStepCount = $totalStepCount;
        $addNumSubRoute = $totalStepCount; // This will serve as the starting number for the sub route track points

        // Display first the header
        $trackDocInfo = $trackDocInfo . "<div class='trackHeaders'>Initial Route</div>";

        // DISPLAY TRACK DOCUMENT
        $printAllYlw = false; // This was used during identifying of subroutes to print all track circles in yellow 
        $tempString = displayTrackDocument($documentId, 1, $stepIdStorage, $currentStep, $totalStepCount, $circleVersion, $circleWidth, $circleHeight, $lineWidth, $lineHeight, $printAllYlw, $ctgryStorage);
        $trackDocInfo = $trackDocInfo . $tempString;

        $trackDocInfo = $trackDocInfo . "</td>";
        $trackDocInfo = $trackDocInfo . "<td style='padding-left: 0px; padding-right: 0px; border: 0px;'>"; // Extra td
        $trackDocInfo = $trackDocInfo . "</td>";
        $trackDocInfo = $trackDocInfo . "</tr>"; // End of first row

        // Start - This part will now be the querying for subroutes if needed
        // 
        // Check if the document is in initial route
        $theSubRoutes = array();
        // Start - If the document was cancelled or rejected in initial route, then don't show the subroutes
        if ( $rejOrCnclInInit != true ) {
            if ( inInitRoute($documentId) == true ) {
                // If the document is in initial route, query the subroutes
                $query =
                "
                    SELECT RS.subRouteId as subRouteId
                    FROM
                        (SELECT RS.routeId as routeId
                        FROM MainDocument as MD, RouteStep as RS
                        WHERE MD.mainDocumentId = '$documentId' AND MD.currentStepId = RS.stepId) as TheRouteId, RouteStep as RS
                    WHERE TheRouteId.routeId = RS.routeId AND RS.subRouteId NOT LIKE '%CANCEL%' AND RS.subRouteId NOT LIKE '%REJECT%' AND RS.subRouteId IS NOT NULL
                    GROUP BY subRouteId
                ";
            }
            else {
                // If the document is in subroute, query that subroute
                $query = 
                "
                    SELECT RS.subRouteId as subRouteId
                    FROM MainDocument as MD, RouteStep as RS
                    WHERE MD.mainDocumentId = '$documentId' AND MD.currentStepId = RS.stepId
                ";
            }

            $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

            while ($row = mysql_fetch_array($result)) {
                $theSubRoutes[] = $row['subRouteId'];
            }

            // If the subroutes are greater or equal to 2, then have a flag to print all the track points in yellow
            $printAllYlw = false;
            if ( count($theSubRoutes) >= 2 ) {
                $printAllYlw = true;
            }

            // Done preparing the $theSubRoutes array

            // Start - Query now the steps for the track points
            foreach ( $theSubRoutes as $aSubRoute) {
                $theSubRouteName = getSubRouteName($aSubRoute);

                $benchMarkStepId = "";
                $upperUnion = "";
                $lowerUnion = "";

                if ( hereActionSubRoute($documentId, $aSubRoute, $theAction) == true ) {
                    // Get first the stepId that will serve as the benchmark to determine the value the stepId will be less than
                    if ( isRejected($documentId) == true ) {
                        $benchMarkStepId = getStepId($documentId, "Rejected");
                        $theSubRouteId = "REJECTTRAC";
                    }
                    else if ( isCancelled($documentId) == true ) {
                        $benchMarkStepId = getStepId($documentId, "Cancelled");
                        $theSubRouteId = "CANCELTRAC";
                    }
                    else {

                    }

                    // Start - Prepare the variables for union
                    $upperUnion = "( ";
                    $benchMarkStepId = "AND stepId <= ".$benchMarkStepId;
                    $lowerUnion = 
                    "
                    )
                    UNION
                    (
                        -- Filter route steps, that is within the subroute
                        SELECT *
                        FROM 

                            -- Get all route steps
                            (SELECT mainDocumentId, currentStepId, WithRouteId.routeId as routeId, stepId, subRouteId, category
                            FROM

                                -- Get routeId
                                (SELECT mainDocumentId, currentStepId, routeId
                                FROM MainDocument as MD, RouteStep as RS
                                WHERE MD.currentStepId = RS.stepId AND mainDocumentId = '$documentId') as WithRouteId

                            LEFT JOIN RouteStep as RS
                            ON WithRouteId.routeId = RS.routeId) as UnfilteredRouteStep

                        WHERE subRouteId = '$theSubRouteId'
                        ORDER BY stepId
                    )
                    ";
                    // End - Prepare the variables for union
                }

                // Prepare the query for the circles
                // Query the subroute, along with the reject and cancel points if needed.
                $query =
                "
                    ".$upperUnion."
                    -- Filter route steps, that is within the subroute
                    SELECT *
                    FROM 

                        -- Get all route steps
                        (SELECT mainDocumentId, currentStepId, WithRouteId.routeId as routeId, stepId, subRouteId, category
                        FROM

                            -- Get routeId
                            (SELECT mainDocumentId, currentStepId, routeId
                            FROM MainDocument as MD, RouteStep as RS
                            WHERE MD.currentStepId = RS.stepId AND mainDocumentId = '$documentId') as WithRouteId

                        LEFT JOIN RouteStep as RS
                        ON WithRouteId.routeId = RS.routeId) as UnfilteredRouteStep

                    WHERE subRouteId = '$aSubRoute' ".$benchMarkStepId."
                    ORDER BY stepId
                    ".$lowerUnion."
                ";
                $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

                // Prepare to add the track points
                // Determine total number of steps, and current step of the document
                $totalStepCount = 0;
                $currentStep = 0;
                $contCheck = true;
                $stepIdStorage = array();
                $ctgryStorage = array();

                while ($row = mysql_fetch_array($result)) {
                    $totalStepCount++;

                    // Store the stepId which will be used for the id of the green/grey circles
                    array_push($stepIdStorage, $row['stepId']);
                    $ctgryStorage[] = $row['category']; // Store the category

                    if( ( $row['currentStepId'] == $row['stepId'] ) && ( $contCheck == true ) ) {
                        $currentStep = $totalStepCount;
                        $contCheck = false;
                    }
                }

                // Add now the track points
                $trackDocInfo = $trackDocInfo . "<tr>"; // Start of row
                $trackDocInfo = $trackDocInfo . "<td valign='top' style='text-align: right; padding-left: 0px; padding-right: 0px; border: 0px;'>"; // Extra td
                $trackDocInfo = $trackDocInfo . " <img style='padding-top: ".$ylwArrwPadTop."px; padding-left: ".$ylwArrwPadLeft."px;' src='images/trackDoc/version5/line/blck_arrow.png' width='".$ylwArrwWidth."' height='".$ylwArrwHeight."'/>";
                $trackDocInfo = $trackDocInfo . "</td>";
                $trackDocInfo = $trackDocInfo . "<td style='text-align:center; padding-left: 0px; padding-right: 0px; border: 0px; padding-bottom: 10px; '>"; // SubRoute TD

                // Display first the header
                $trackDocInfo = $trackDocInfo . "<div class='trackHeaders'>".$theSubRouteName."</div>";

                // DISPLAY TRACK DOCUMENT
                $tempString = displayTrackDocument($documentId, $addNumSubRoute+1, $stepIdStorage, $currentStep, $totalStepCount, $circleVersion, $circleWidth, $circleHeight, $lineWidth, $lineHeight, $printAllYlw, $ctgryStorage);
                $trackDocInfo = $trackDocInfo . $tempString;
                $trackDocInfo = $trackDocInfo . "</td>";
                $trackDocInfo = $trackDocInfo . "</tr>"; // End of row

                $archiveTotalStepCount = $archiveTotalStepCount + $totalStepCount; // Add the totalStepCount
            } // End - Query now the steps for the track points

        } // End - If the document was cancelled or rejected in initial route, then don't show the subroutes

        // Since everything is setup, change the $totalStepCount to $archiveTotalStepCount,
        // so that the succeeding codes will still work
        $totalStepCount = $archiveTotalStepCount;

        // End - This part will now be the querying for subroutes if needed

        $trackDocInfo = $trackDocInfo . "</table>"; // End of outer table
    
        
        echo $trackDocInfo;
        // THIS DIV WILL BE USED FOR SHOWING OF DOCUMENT INFORMATION DEPENDING ON THE HOVERED CIRCLE
        echo "<div class='trackInvisibleInfo' style='display:none;'></div>";

    ?>
    </div></div>
    
    <!-- Add the input box for hiding the scroll if necessary -->
    <input type="hidden" value="<?php echo $totalStepCount; ?>" name="divInptScroll[]" id="divInptScroll__<?php echo $documentId; ?>"/>
    </td>
<!-- END - ##############DISPLAY TRACK DOCUMENT############## -->
                
